package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.context.BaseContext;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.pojos.ApArticle;
import com.heima.article.service.ApDataStatisticsService;
import com.heima.common.exception.CustomException;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.wemedia.vos.WmNewsStatisticsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ApDataStatisticsServiceImpl implements ApDataStatisticsService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    /**
     * 数据统计
     *
     * @param authorId 作者id
     * @return 返回结果
     */
    @Override
    public Map<String, Integer> statistics(Integer authorId, Date beginDate, Date endDate) {
        if (authorId == null) {
            return null;
        }
        int likesNum = 0;//点赞量
        int collectionNum = 0;//收藏量
        LambdaQueryWrapper<ApArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApArticle::getAuthorId, authorId)
                .ge(ApArticle::getPublishTime, beginDate)
                .le(ApArticle::getPublishTime, endDate)
                .orderByDesc(ApArticle::getCreatedTime);
        List<ApArticle> apArticles = apArticleMapper.selectList(wrapper);
        for (ApArticle apArticle : apArticles) {
            Integer likes = apArticle.getLikes();
            Integer collection = apArticle.getCollection();
            likesNum = (likes == null ? likesNum : likes + likesNum);
            collectionNum = (collection == null ? collectionNum : collection + collectionNum);
        }
        HashMap<String, Integer> data = new HashMap<>();
        data.put("likesNum", likesNum);
        data.put("collectNum", collectionNum);
        return data;
    }

    /**
     * 分页查询发布文章相关信息
     *
     * @param beginDate 开始时间
     * @param endDate   结束时间
     * @param page      页码
     * @param size      显示条数
     * @return 返回结果
     */
    @Override
    public Page<WmNewsStatisticsVo> articlePage(Date beginDate, Date endDate, Integer page, Integer size, Integer apUserId) {
        if (beginDate == null ||
                endDate == null ||
                page == null ||
                size == null ||
                apUserId == null
        ) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //分页查询文章信息
        Page<ApArticle> apArticlePage = new Page<>(page, size);
        apArticleMapper.selectArticleByDateInterval(apArticlePage, beginDate, endDate, apUserId);//查找条件
        long total = apArticlePage.getTotal();//总条数
        //封装前端需要的page
        Page<WmNewsStatisticsVo> wmNewsStatisticsVoPage = new Page<>();

        if (total != 0) {
            List<ApArticle> records = apArticlePage.getRecords();
            List<WmNewsStatisticsVo> wmNewsStatisticsVoList = records.stream().map(apArticle -> {
                WmNewsStatisticsVo wmNewsStatisticsVo = new WmNewsStatisticsVo();
                wmNewsStatisticsVo.setId(apArticle.getId().toString());
                wmNewsStatisticsVo.setTitle(apArticle.getTitle());
                wmNewsStatisticsVo.setLikes(apArticle.getLikes() == null ? 0 : apArticle.getLikes());
                wmNewsStatisticsVo.setCollection(apArticle.getCollection() == null ? 0 : apArticle.getCollection());
                wmNewsStatisticsVo.setComment(apArticle.getComment() == null ? 0 : apArticle.getComment());
                wmNewsStatisticsVo.setViews(apArticle.getViews() == null ? 0 : apArticle.getViews());
                return wmNewsStatisticsVo;
            }).collect(Collectors.toList());

            wmNewsStatisticsVoPage.setCurrent(page);
            wmNewsStatisticsVoPage.setSize(size);
            wmNewsStatisticsVoPage.setRecords(wmNewsStatisticsVoList);
            wmNewsStatisticsVoPage.setTotal(total);
        }

        return wmNewsStatisticsVoPage;
    }
}
